/*
 * Store.java
 * Team qq 2011
 */
package com.google.code.timetrail.backend;
/**
 * implements the Stores that will be used in the trail
 * @author Steven
 * @version 1.0 
 * As of now stores have unlimited inventory but that might change in the near future
 * 
 * The way this works is you do all the displaying and stuff with the temporary values
 * then when next is clicked you run it all 4 REALZ and then commit the inventory back to 
 * controller
 */
public class Store {
	private Inventory playerInv;

	private int tempMoney, tempFuelCells, tempFood, tempHullParts, tempFCapacitors, tempEngines, tempTimeBulletBills, tempTimesuits;

	private Item money, fuelCells, food, hullParts, fCapacitors, engines, timeSuits, timeBulletBills;

	public Store(Control cont){
		playerInv = cont.getInv();
		tempMoney = 0;
		tempFuelCells = 0;
		tempFood = 0;
		tempHullParts = 0;
		tempFCapacitors = 0;
		tempEngines = 0;
		
		money = new Money();
		fuelCells = new FuelCell();
		food = new Food();
		hullParts = new HullPart();
		fCapacitors = new FluxCapacitor();
		engines = new Engine();
		timeSuits = new TimeSuit();
		timeBulletBills = new TimeBulletBill();
	}

	/**
	 * adds an item to the inventory while factoring in cost
	 * PRECONDITION:THE MONEY IS NOT AN ISSUE
	 * @param amount
	 * @param item
	 * @param inv
	 * @return addItem's return if there is enough money, 0 if there is not
	 */
	public boolean buyItem(int amount, Item item){
		boolean itemCheck = playerInv.addItem(amount, item, true);
		boolean moneyCheck = playerInv.getMoney().removeCurrentAmount(amount*item.getValue(), true);
		if(itemCheck && moneyCheck){
			playerInv.addItem(amount, item);
			playerInv.getMoney().removeCurrentAmount(amount*item.getValue());
		}
		return itemCheck && moneyCheck;
			

	}
	
	public boolean buyItem(int amount, Item item, boolean dryRunFlag){
		return (playerInv.addItem(amount, item, true) 
				&& playerInv.getMoney().removeCurrentAmount(amount*item.getValue(), true)); 
	}

	/**
	 * Possibility to add soon, currently not in use
	 * PRECONDITION MONEY IS NOT AN ISSUE
	 * removes an item from the inventory while also giving the player some money
	 * @param amount amount of items to be removed from the inventory 
	 * @param item the type of item to remove
	 * @param inv the inventory you are selling to
	 * @return removeItem's return unless the other inventory has insufficient money
	 */
	public boolean sellItem(int amount, Item item){
		return false;
	}
	
	public void commitInventoryChanges(Control cont){
		cont.setInv(playerInv);
	}
	
	/**
	 * getter for tempMoney
	 * @return tempMoney
	 */
	public int getTempMoney() {
		return tempMoney;
	}

	/**
	 * setter for tempMoney
	 * @param tempMoney
	 */
	public void setTempMoney(int tempMoney) {
		this.tempMoney = tempMoney;
	}

	/**
	 * getter for tempFuelCells
	 * @return tempFuelCells
	 */
	public int getTempFuelCells() {
		return tempFuelCells;
	}

	/**
	 * setter for tempFuelCells
	 * @param tempFuelCells
	 */
	public void setTempFuelCells(int tempFuelCells) {
		this.tempFuelCells = tempFuelCells;
	}

	/**
	 * getter for TempFood
	 * @return tempFood
	 */
	public int getTempFood() {
		return tempFood;
	}

	/**
	 * setter for tempFood
	 * @param tempFood
	 */
	public void setTempFood(int tempFood) {
		this.tempFood = tempFood;
	}

	/**
	 * getter for TempHullParts
	 * @return tempHullParts
	 */
	public int getTempHullParts() {
		return tempHullParts;
	}
	
	/**
	 * setter for TempHullParts
	 * @param tempHullParts
	 */
	public void setTempHullParts(int tempHullParts) {
		this.tempHullParts = tempHullParts;
	}
	
	/**
	 * getter for tempFCapacitors
	 * @return tempFCapacitors
	 */
	public int getTempFCapacitors() {
		return tempFCapacitors;
	}
	
	/**
	 * setter for tempFCapacitors
	 * @param tempFCapacitors 
	 */
	public void setTempFCapacitors(int tempFCapacitors) {
		this.tempFCapacitors = tempFCapacitors;
	}
	
	/**
	 * getter for tempEngines
	 * @return tempEngines
	 */
	public int getTempEngines() {
		return tempEngines;
	}

	/**
	 * setter for tempEngines
	 * @param tempEngines
	 */
	public void setTempEngines(int tempEngines) {
		this.tempEngines = tempEngines;
	}
	
	public void setTempTimeBulletBills(int tempTimeBulletBills){
		this.tempTimeBulletBills = tempTimeBulletBills;
	}
	
	public void setTempTimeSuits(int tempTimeSuits){
		this.tempTimesuits = tempTimeSuits;
	}

	/**
	 * sums up the current temporary weight added to the actual weight
	 * @return the sum of all the temp weights added to the current weight
	 */
	public int getTempWeight() {
		return playerInv.getInvWeight()+((tempMoney*money.getWeight())+(tempFuelCells*fuelCells.getWeight())+(tempFood*food.getWeight()) 
				+(tempHullParts*hullParts.getWeight())+(tempFCapacitors*fCapacitors.getWeight()) 
				+(tempEngines*engines.getWeight())+tempTimesuits*timeSuits.getWeight() + tempTimeBulletBills*timeBulletBills.getWeight());
	}
	
	/**
	 * Sums the balance temporarily for gui purposes
	 * @return the summation of the temporary variables minus the current money amount
	 */
	public int getTempCost() {
		return playerInv.getMoney().getCurrentAmount()-((tempMoney*money.getValue())+(tempFuelCells*fuelCells.getValue())
				+(tempFood*food.getValue())+(tempHullParts*hullParts.getValue())+(tempFCapacitors*fCapacitors.getValue()) 
				+(tempEngines*engines.getValue())+tempTimesuits*timeSuits.getValue() + tempTimeBulletBills*timeBulletBills.getValue());
	}
	

}
